<?php
/**
 * Geneone: Content Management Framework
 * Import Profile
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once 'Validate.php';
require_once 'Gene/User/FOAF.php';

function getFoafFromWeb($url) {
	if (Validate::uri($url, array("allowed_schemes" => array("http", "https"), "strict" => ""))) {
		/* Check URL is not private */
		$parsedurl = parse_url($url);
		$ip = gethostbyname($parsedurl['host']);
		$long = ip2long($ip);
		
		// 167772160 - 10.0.0.0
		// 184549375 - 10.255.255.255
		//
		// -1408237568 - 172.16.0.0
		// -1407188993 - 172.31.255.255
		//
		// -1062731776 - 192.168.0.0
		// -1062666241 - 192.168.255.255
		//
		// -1442971648 - 169.254.0.0
		// -1442906113 - 169.254.255.255
		//
		// 2130706432 - 127.0.0.0
		// 2147483647 - 127.255.255.255 (32 bit integer limit!!!)
		//
		// -1 is also b0rked
		
		if (($long >= 167772160 AND $long <= 184549375) OR ($long >= -1408237568 AND $long <= -1407188993) OR ($long >= -1062731776 AND $long <= -1062666241) OR ($long >= 2130706432 AND $long <= 2147483647) OR $long == -1) {
			return false;
		}
		
		$request =& Gene::getHttpRequest($url);
		$request->setMethod(HTTP_REQUEST_METHOD_GET);

		if (PEAR::isError($request->sendRequest())) {
			return false;
		} else {
			$response = $request->getResponseBody();

			// Follow metadata link tags
			if (preg_match('/<link rel=\"([^"]+)\" type=\"application\/rdf\+xml\" href=\"([^"]+)\" title=\"FOAF\"[ \/]?/', $response, $m)) {
				$foafurl = $m['2'];

				if (Validate::uri($foafurl, array("allowed_schemes" => array("http", "https"), "strict" => ""))) {
					$request->setURL($foafurl);

					if (PEAR::isError($request->sendRequest())) {
						return false;
					}

					$response = $request->getResponseBody();
				} else {
					$response = '';
				}
			}
			
			// Check for a valid MIME type
			$type = $request->getResponseHeader("Content-Type");
			if (in_array($type, array("application/octet-stream", "application/rdf+xml", "application/xml", "text/xml", "text/plain"))) {
				return $response;
			} else {
				return false;
			}
		}
	} else {
		return false;
	}
}

function getFoafFromUpload($uploadname) {
	if (isset($_FILES[$uploadname])) {
		// Firefox 1.5 saves FOAF files default as html which will upload with wrong MIME type
		
		//if (!in_array($_FILES[$uploadname]['type'], array("application/octet-stream", "application/rdf+xml", "application/xml", "text/xml", "text/plain"))) {
		//	return false;
		//}
		
		if ($contents = file_get_contents($_FILES[$uploadname]['tmp_name'])) {
			return $contents;
		}
		
		return false;
	} else {
		return false;
	}
}
 
class Gene_Special_Import_Profile {
	/**
	 * Default Action
	 */
	function main() {
		$tpl =& Gene::getTemplate();
		$tpl->assign ("pagetitle", "Profile Import");
		$type = Gene_Request::getString("type", GENE_REQUEST_GET);
		if ($type == "file") {
			$tpl->assign ("method", "file");
		} else {
			$tpl->assign ("method", "website");
		}

		$tpl->assign ("finalbody", $tpl->fetch("special/importprofile"));
	}
	
	/**
	 * Default Action
	 */
	function import() {
		$tpl =& Gene::getTemplate();
		$tpl->assign ("pagetitle", "Profile Import");
		$type = Gene_Request::getString("type", GENE_REQUEST_POST);
		
		if ($type == "file") {
			$tpl->assign ("method", "file");
		} else {
			$tpl->assign ("method", "website");
		}
		
		$profilemap = array(
			"foaf_gender" => "gender",
			"foaf_homepage" => "homepage",
			"foaf_jabberID" => "jabber",
			"profile_location" => "location",
			"profile_bio" => "bio",
		);
		
		if ($type == "website") {
			$url = Gene_Request::getString("foafurl", GENE_REQUEST_POST);
			
			if (!$url || !Validate::uri($url, array("allowed_schemes" => array("http", "https"), "strict" => ""))) {
				$tpl->assign("errormsg", "Please make sure you enter a valid web address.");
				$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
			} else {			
				if ($foaf = getFoafFromWeb($url)) {
					if ($data = Gene_User_FOAF::parse($foaf)) {
					$tpl->assign("imported", true);
					
					$imported = array();
					foreach ($data as $j => $k) {
						if (isset($profilemap[$j])) {
							$imported[$profilemap[$j]] = $k;
						}
						
					}
					if (isset($imported['gender'])) {
						$imported['gender'] = substr($imported['gender'], 0, 1);
					}
					
					$user =& Gene::GetUser();
					$info = $user->getInfo();
					$tpl->assign("pagetitle", $info['name']);
					$tpl->assign("profile", array_merge($info, $imported));
					
					$tpl->assign("finalbody", $tpl->fetch("user/editprofile"));
					} else {
						$tpl->assign("errormsg", "Sorry, it looks like the file you uploaded was not a valid profile.");
						$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
					}
				} else {
					$tpl->assign("errormsg", "Sorry, we could not connect to the server to download your profile. Check for typing mistakes, or try uploading your profile.");
					$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
				}
			}
		} else if ($type == "file") {
			if ($foaf = getFoafFromUpload("foaffile")) {
				if ($data = Gene_User_FOAF::parse($foaf)) {
					$tpl->assign("imported", true);
					
					$imported = array();
					foreach ($data as $j => $k) {
						if (isset($profilemap[$j])) {
							$imported[$profilemap[$j]] = $k;
						}
						
					}
					if (isset($imported['gender'])) {
						$imported['gender'] = substr($imported['gender'], 0, 1);
					}
					
					$user =& Gene::GetUser();
					$info = $user->getInfo();
					$tpl->assign("pagetitle", $info['name']);
					$tpl->assign("profile", array_merge($info, $imported));
					
					$tpl->assign("finalbody", $tpl->fetch("user/editprofile"));
				} else {
					$tpl->assign("errormsg", "Sorry, it looks like the file you uploaded was not a valid profile.");
					$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
				}
			} else {
				$tpl->assign("errormsg", "Sorry, we could not open your profile. Please make sure you uploaded a valid profile.");
				$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
			}
		} else {
			$tpl->assign("errormsg", "We cannot import a profile using this method.");
			$tpl->assign("finalbody", $tpl->fetch("special/importprofile"));
		}
	}
}

?>